//
//  DebugMainViewLayout.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/23.
//

#import "DebugMainViewLayout.h"

@interface DebugMainViewLayout ()

///所有attributes
@property(nonatomic,strong) NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributes;
///
@property(nonatomic,assign) CGFloat lastMaxY;
///
@property(nonatomic,assign) CGFloat maxHeight;

@end

@implementation DebugMainViewLayout
//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        self.scrollDirection = UICollectionViewScrollDirectionVertical;
//    }
//    return self;
//}
- (void)prepareLayout
{
    [super prepareLayout];
    NSInteger sctions = [self.collectionView numberOfSections];
    
    self.lastMaxY = 0;
    
    for (NSInteger i = 0; i < sctions; i++) {
        UICollectionViewLayoutAttributes * layoutHeader = [UICollectionViewLayoutAttributes   layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
          layoutHeader.frame =CGRectMake(0,self.lastMaxY, UIScreen.mainScreen.bounds.size.width, 20);
        [self.layoutAttributes addObject:layoutHeader];
        self.lastMaxY = self.lastMaxY + 20;
        NSInteger itemCount = [self.collectionView numberOfItemsInSection:i];
        // 获取所有布局
        for (NSInteger j = 0; j < itemCount; j++) {
            NSIndexPath *indexpath = [NSIndexPath indexPathForItem:j inSection:i];
            UICollectionViewLayoutAttributes *attr = [self layoutAttributesForItemAtIndexPath:indexpath];
            [self.layoutAttributes addObject:attr];
            if (j == itemCount -1) {
                self.lastMaxY = CGRectGetMaxY(attr.frame);
            }
        }
        if (i == sctions -1) {
            self.maxHeight = self.lastMaxY;
        }
    }
    
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.layoutAttributes;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewLayoutAttributes *attri = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat attriX = 0;
    CGFloat attriY = self.lastMaxY;
    CGFloat attriWidth = 0;
    CGFloat attriHeigh = 0;
    NSInteger line;
    NSInteger lineItem;
    if (indexPath.section == 0) {
        lineItem = indexPath.item % 3;
        line = indexPath.item / 3;
        attriWidth = UIScreen.mainScreen.bounds.size.width / 3;
        attriHeigh = 50;
        attriY = attriY + line * attriHeigh;
        attriX = lineItem * attriWidth;
    }else
    {
        lineItem = indexPath.item % 4;
        line = indexPath.item / 4;
        attriWidth = UIScreen.mainScreen.bounds.size.width / 4;
        attriHeigh = attriWidth;
        attriY = attriY + line * attriHeigh;
        attriX = lineItem * attriWidth;
    }
    attri.frame = CGRectMake(attriX, attriY, attriWidth, attriHeigh);
    return attri;
}
- (NSMutableArray<UICollectionViewLayoutAttributes *> *)layoutAttributes
{
    if (!_layoutAttributes) {
        _layoutAttributes = [NSMutableArray array];
    }
    return _layoutAttributes;
}
- (CGSize)collectionViewContentSize
{
    return CGSizeMake(UIScreen.mainScreen.bounds.size.width, self.maxHeight);
}
@end
